package com.itheima.remark.controller;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.remark.service.ProductService;
import com.itheima.pojo.Product;
import com.github.pagehelper.Page;
import com.remark.search.pojo.RemarkImfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private ProductService productService;

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Product> productList = productService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",productList) ;
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Product product = productService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",product);
    }


    /***
     * 新增数据
     * @param product
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Product product){
        productService.add(product);
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /***
     * 修改数据
     * @param product
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Product product,@PathVariable Integer id){
        product.setId(id);
        productService.update(product);
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        productService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    public Result findList(@RequestParam Map searchMap){
        List<Product> list = productService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<Product> pageList = productService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /***
     * 分页搜索实现
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/searchInfo/{page}/{size}" )
    public  List<RemarkImfo> findInfoPage( @PathVariable("page")  int page, @PathVariable("size")  int size){
        Page<RemarkImfo> pageList = productService.findInfoPage( page, size);
        List<RemarkImfo> result = pageList.getResult();
        return result;
    }

    /**
     * 获取总数
     * @return
     */
    @GetMapping("/count")
    public Integer count(){
        return productService.count();
    }


    /***
     * 根据id获取remarkImfo
     * @param id
     * @return
     */
    @GetMapping(value = "/searchInfo/{id}" )
    public RemarkImfo findInfoById(@PathVariable("id") Integer id){

        RemarkImfo infoBtId = productService.findInfoBtId(id);
        return infoBtId;
    }


}
